import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_app/page/Whiteboard/WhiteBoardController.dart';

class WhiteboardPage extends GetView<WhiteboardController> {
  final GlobalKey _globalKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    controller.init();
    return Scaffold(
        appBar: AppBar(
            title: Text("画布"),
            leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              color: Color(0xFF333333),
              onPressed: () {
                Get.back();
              },
            )),
        body: controller.obx((state) {
          return GestureDetector(
            onPanEnd: (DragEndDetails details) => controller.add(null),
            onPanUpdate: (DragUpdateDetails details) {
              RenderBox box = _globalKey.currentContext.findRenderObject();
//            RenderBox box = context.findRenderObject();
              var globalToLocal = box.globalToLocal(details.globalPosition);
              controller.add(globalToLocal);
            },
            child: Obx(() {
              return CustomPaint(
                key: _globalKey,
                painter: Line(controller.points.toList()),
                child: Container(),
              );
            }),
          );
        }),
        floatingActionButton: FloatingActionButton(
          onPressed: () => controller.replay(),
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ));
  }
}

class Line extends CustomPainter {
  Path path = Path();
  final List<PointsModel> points;
  Line(this.points);

  @override
  void paint(Canvas canvas, Size size) {
    circle(canvas, size);
//    line(canvas, size);
  }

  circle(Canvas canvas, Size size) {
    Paint line = new Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..strokeWidth = 6
      ..style = PaintingStyle.stroke
      ..strokeJoin = StrokeJoin.round;
    if (points == null) {
      return;
    }
    PointsModel last = points.lastWhere((element) => element != null && element.offset != null, orElse: () => null);
    PointsModel first = points.firstWhere((element) => element != null && element.offset != null, orElse: () => null);
    if (last != null && first != null) {
      canvas.drawOval(Rect.fromPoints(first.offset, last.offset), line);
      canvas.drawRect(Rect.fromPoints(first.offset, last.offset), line);
      Path path = Path();
      path.moveTo(first.offset.dx, first.offset.dy);
      path.lineTo(last.offset.dx, last.offset.dy);
      canvas.drawPath(path, line);
    }
  }

  rect(Canvas canvas, Size size) {
    Paint line = new Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..strokeWidth = 6
      ..style = PaintingStyle.stroke
      ..strokeJoin = StrokeJoin.round;
    if (points == null) {
      return;
    }
    PointsModel last = points.lastWhere((element) => element != null && element.offset != null, orElse: () => null);
    PointsModel first = points.firstWhere((element) => element != null && element.offset != null, orElse: () => null);
    if (last != null && first != null) {
      canvas.drawRect(Rect.fromPoints(first.offset, last.offset), line);
    }
  }

  lineSegment(Canvas canvas, Size size) {
    Paint line = new Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..strokeWidth = 6
      ..style = PaintingStyle.stroke
      ..strokeJoin = StrokeJoin.round;
    if (points == null) {
      return;
    }
    PointsModel last = points.lastWhere((element) => element != null && element.offset != null, orElse: () => null);
    PointsModel first = points.firstWhere((element) => element != null && element.offset != null, orElse: () => null);
    if (last != null && first != null) {
      Path path = Path();
      path.moveTo(first.offset.dx, first.offset.dy);
      path.lineTo(last.offset.dx, last.offset.dy);
      canvas.drawPath(path, line);
    }
  }

  line(Canvas canvas, Size size) {
    Paint mHelpPaint = new Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..strokeWidth = 6
      ..strokeJoin = StrokeJoin.round;
    if (points == null) {
      return;
    }
    for (int i = 0; i < points.length - 1; i++) {
      if (points[i] != null && points[i].offset != null && points[i + 1] != null && points[i + 1].offset != null) {
        canvas.drawLine(points[i].offset, points[i + 1].offset, mHelpPaint);
      }
    }
  }

  @override
  bool shouldRepaint(Line oldDelegate) {
    return oldDelegate.points != points;
  }
}

class StarView extends CustomPainter {
  Paint mHelpPaint;
  final BuildContext context;

  StarView(this.context) {
    mHelpPaint.style = PaintingStyle.stroke;
    mHelpPaint.color = Color(0xffBBC3C5);
    mHelpPaint.isAntiAlias = true;
  }
  @override
  void paint(Canvas canvas, Size size) {
    final winSize = MediaQuery.of(context).size;
    canvas.drawPath(gridPath(20, winSize), mHelpPaint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  Path gridPath(int step, Size winSize) {
    Path path = Path();
    for (int i = 0; i < winSize.height / step + 1; i++) {
      path.moveTo(0, step * i.toDouble());
      path.lineTo(winSize.width, step * i.toDouble());
    }
    for (int i = 0; i < winSize.width / step + 1; i++) {
      path.moveTo(step * i.toDouble(), 0);
      path.lineTo(step * i.toDouble(), winSize.height);
    }
    return path;
  }
}
